//
//  ZJPushTableViewCell.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/28.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJPushTableViewCell.h"

@interface ZJPushTableViewCell ()

/** 背景 */
@property (nonatomic, strong) UIView *bGView;
/** 类型图片 */
@property (nonatomic, strong) UIImageView *typeImageView;
/** title */
@property (nonatomic, strong) UILabel *titleLabel;
/** 时间 */
@property (nonatomic, strong) UILabel *timeLabel;
/** 内容图片 */
@property (nonatomic, strong) UIImageView *contentImageView;
/** 描述内容 */
@property (nonatomic, strong) UILabel *contentLabel;
/** 分割线 */
@property (nonatomic, strong) UILabel *spaceLine;
/** 查看label */
@property (nonatomic, strong) UILabel *lookLabel;
/** 小箭头 */
@property (nonatomic, strong) UIImageView *assrow;

/** 未读标记 */
@property (nonatomic, strong) UIImageView *markImageView;
@end

@implementation ZJPushTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.bGView];
        [self.bGView addSubview:self.typeImageView];
        [self.bGView addSubview:self.titleLabel];
        [self.bGView addSubview:self.timeLabel];
        [self.bGView addSubview:self.contentImageView];
        [self.bGView addSubview:self.contentLabel];
        [self.bGView addSubview:self.spaceLine];
        [self.bGView addSubview:self.lookLabel];
        [self.bGView addSubview:self.assrow];
        [self.bGView addSubview:self.markImageView];

        self.bGView.sd_layout
        .topSpaceToView(self.contentView, 10 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .bottomSpaceToView(self.contentView, 10 * [UIScreen zj_screenHScale]);
        
        self.typeImageView.sd_layout
        .topSpaceToView(self.bGView, 5 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self.bGView, 10 * [UIScreen zj_screenHScale])
        .widthIs(40 * [UIScreen zj_screenWScale])
        .heightEqualToWidth();
    
        self.markImageView.sd_layout
        .topSpaceToView(self.bGView, 1 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self.typeImageView, - 7 * [UIScreen zj_screenHScale])
        .widthIs(11 * [UIScreen zj_screenHScale])
        .heightEqualToWidth();
        
        self.titleLabel.sd_layout
        .topEqualToView(self.typeImageView)
        .leftSpaceToView(self.typeImageView, 8 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.bGView, 10 * [UIScreen zj_screenHScale])
        .heightIs([NSString heightWithTextFont:self.titleLabel.font]);
        
        self.timeLabel.sd_layout
        .bottomEqualToView(self.typeImageView)
        .leftEqualToView(self.titleLabel)
        .rightEqualToView(self.titleLabel)
        .heightIs([NSString heightWithTextFont:self.timeLabel.font]);
        
        self.contentImageView.sd_layout
        .topSpaceToView(self.typeImageView, 5 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.typeImageView)
        .rightEqualToView(self.titleLabel)
        .heightIs(200 * [UIScreen zj_screenHScale]);
        
        self.contentLabel.sd_layout
        .topSpaceToView(self.contentImageView, 5 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.contentImageView)
        .widthIs([UIScreen zj_screenWith] - 24 * [UIScreen zj_screenWScale] - 20 * [UIScreen zj_screenHScale])
        .heightIs(40);
        
        self.spaceLine.sd_layout
        .topSpaceToView(self.contentLabel, 3 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.contentLabel)
        .rightEqualToView(self.contentLabel)
        .heightIs(1);
        
        self.lookLabel.sd_layout
        .topSpaceToView(self.spaceLine, 5 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.spaceLine)
        .widthIs(200 * [UIScreen zj_screenWScale])
        .heightIs([NSString heightWithTextFont:self.lookLabel.font]);
        
        self.assrow.sd_layout
        .topEqualToView(self.lookLabel)
        .rightEqualToView(self.spaceLine)
        .heightRatioToView(self.lookLabel, 1)
        .widthEqualToHeight();
        
        
        
    }
    return self;
}





#pragma mark - Setter
- (void)setModel:(ZJPushInfoModel *)model{
    _model = model;
    self.titleLabel.text = model.title;
    //给time赋值
    NSDateFormatter *dateformatter = [NSDateFormatter new];
    dateformatter.dateFormat = @"MM月dd日 HH:mm";
    NSArray *array = [model.createDate componentsSeparatedByString:@"."];
    self.timeLabel.text = [dateformatter stringFromDate: [NSDateFormatter dateTimeWithString:array[0]]];
    
    self.contentLabel.text = model.desc ? model.desc:@"";
    if (model.cover) {//封面存在
        if ([model.cover compare:@"http"]) {
            [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:PlaceholderBIGImage];
        }else{
            [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEIMAGEURL,model.cover]] placeholderImage:PlaceholderBIGImage];
        }
        
        self.contentImageView.hidden = NO;
        self.contentLabel.sd_layout
        .topSpaceToView(self.contentImageView, 5 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.contentImageView)
        .widthIs([UIScreen zj_screenWith] - 24 * [UIScreen zj_screenWScale] - 20 * [UIScreen zj_screenHScale])
        .heightIs([self.contentLabel getLableSizeWithMaxWidth:self.contentLabel.width].height);
    }else{
        self.contentImageView.hidden = YES;
        self.contentLabel.sd_layout
        .topSpaceToView(self.typeImageView, 5 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.contentImageView)
        .widthIs([UIScreen zj_screenWith] - 24 * [UIScreen zj_screenWScale] - 20 * [UIScreen zj_screenHScale])
        .heightIs([self.contentLabel getLableSizeWithMaxWidth:self.contentLabel.width].height);
    }
    [self.contentLabel updateLayout];
    //是否阅读
    if ([model.state isEqualToString:@"1"]) {
        self.markImageView.hidden = YES;
        self.lookLabel.text = @"已查看";
        self.lookLabel.textColor = [UIColor zj_colorWithHexString:@"808080"];
        self.titleLabel.textColor = [UIColor zj_colorWithHexString:@"7a7a7a"];
    }else{
        self.markImageView.hidden = NO;
        self.lookLabel.text = @"点击查看";
        self.lookLabel.textColor = [UIColor zj_colorWithHexString:@"333333"];
        self.titleLabel.textColor = [UIColor zj_colorWithHexString:@"333333"];
    }
    
    
    /**  0、链接 1、学习  2、活动  3、直播 4、新闻 5、需求  6、资源 */
    if ([model.type isEqualToString:@"0"]) {
        self.typeImageView.image = [UIImage imageNamed:@"news_tuijian"];
    }else if ([model.type isEqualToString:@"1"]) {
        self.typeImageView.image = [UIImage imageNamed:@"news_tuijian"];
    }else if ([model.type isEqualToString:@"2"]) {
        self.typeImageView.image = [UIImage imageNamed:@"news_tuijian"];
    }else if ([model.type isEqualToString:@"3"]) {
        self.typeImageView.image = [UIImage imageNamed:@"news_tuijian"];
    }else if ([model.type isEqualToString:@"4"]) {
        self.typeImageView.image = [UIImage imageNamed:@"news_tuijian"];
    }else if ([model.type isEqualToString:@"5"]) {
        self.typeImageView.image = [UIImage imageNamed:@"news_tuijian"];
    }else if ([model.type isEqualToString:@"6"]) {
        self.typeImageView.image = [UIImage imageNamed:@"news_tuijian"];
    }else{
        self.typeImageView.image = [UIImage imageNamed:@"news_tuijian"];
    }
}

#pragma mark - Getters
- (UIView *)bGView{
    if (_bGView == nil){
        _bGView = [UIView new];
        _bGView.backgroundColor = [UIColor whiteColor];
        _bGView.layer.cornerRadius = 4 * [UIScreen zj_screenHScale];
        _bGView.layer.borderWidth = 1;
        _bGView.layer.borderColor = [UIColor viewBGGrayColor].CGColor;
    }
    return _bGView;
}

- (UIImageView *)typeImageView{
    if (_typeImageView == nil){
        _typeImageView = [UIImageView new];
        _typeImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _typeImageView;
}

- (UILabel *)titleLabel{
    if (_titleLabel == nil){
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor textBlackColor];
        _titleLabel.font = [UIFont titleTextFont];
    }
    return _titleLabel;
}

- (UILabel *)timeLabel{
    if (_timeLabel == nil){
        _timeLabel = [UILabel new];
        _timeLabel.textColor = [UIColor zj_colorWithHexString:@"a8a8a8"];
        _timeLabel.font = [UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]];
    }
    return _timeLabel;
}
- (UIImageView *)contentImageView{
    if (_contentImageView == nil){
        _contentImageView = [UIImageView new];
        _contentImageView.contentMode = UIViewContentModeScaleAspectFill;
        _contentImageView.clipsToBounds = YES;
    }
    return _contentImageView;
}

- (UILabel *)contentLabel{
    if (_contentLabel == nil){
        _contentLabel = [UILabel new];
        _contentLabel.textColor = self.timeLabel.textColor;
        _contentLabel.font = [UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]];
        _contentLabel.numberOfLines = 0;
        _contentLabel.characterSpace = 1;
        _contentLabel.lineSpace = 2 * [UIScreen zj_screenHScale];
    }
    return _contentLabel;
}

- (UILabel *)spaceLine{
    if (_spaceLine == nil){
        _spaceLine = [UILabel new];
        _spaceLine.backgroundColor = [UIColor viewBGGrayColor];
    }
    return _spaceLine;
}
- (UILabel *)lookLabel{
    if (_lookLabel == nil){
        _lookLabel = [UILabel new];
        _lookLabel.textColor = [UIColor textBlackColor];
        _lookLabel.font = [UIFont subTitleTextFont];
        _lookLabel.text = @"点击查看";
    }
    return _lookLabel;
}
- (UIImageView *)assrow{
    if (_assrow == nil){
        _assrow = [UIImageView new];
        _assrow.contentMode = UIViewContentModeCenter;
        _assrow.image = [UIImage imageNamed:@"jiantou_pop"];
    }
    return _assrow;
}

- (UIImageView *)markImageView{
    if (_markImageView == nil){
        _markImageView = [UIImageView new];
        _markImageView.image = [UIImage imageNamed:@"icon_circle"];
    }
    return _markImageView;
}
@end
